/**
 * @class SQL搜索器。
 */
$$.sql.Searcher = {
	/**
	 * 简易分词生成器
	 * @cfg {Array} targetField 选择要搜索的字段（最多两个）
	 * @cfg {Boolean} isPreSearch 可选的 默认false 返回找到的记录集SQL语句
	 * @cfg {String} tablename 选择的表名
	 * @return {String} SQL字符串
	 */
	searchParser: function (config){
		var keyword = config.keyword;
		if (!keyword)
			throw new Error(3003, '查找的内容不能为空！');
		var len = keyword.length;
		var subkey = '';
		var s1 = [], s2 = [];
		var lensubkey = 2;
		var targetField_1 = config.targetField[0];
		var targetField_2 = config.targetField[1];

		switch (len) {
			case 0 :
				throw '查找的内容不能为空！';
				break;
			case 1 :
				// 若长度为1，则不设任何值
				break;
			default :
				// 若长度大于1，则从字符串首字符开始，循环取长度为2的子字符串作为查询条件
				for (var i = 0, j = len - (lensubkey - 1); i < j; i++) {
					s1.push(' or ' + targetField_1 + ' like \'%'
							+ keyword.substr(i, lensubkey) + '%\'');
					s2.push(' or ' + targetField_2 + ' like \'%'
							+ keyword.substr(i, lensubkey) + '%\'');
				}
				break;
		}
		return config.isPreSearch
				? "SELECT count(*) AS TotalCount FROM " + config.tablename
						+ "" + " where " + targetField_1 + " like '%" + keyword
						+ "%' " + " or " + targetField_2 + " like '%" + keyword
						+ "%'" + s1.join('') + s2.join('')
				: "SELECT * FROM " + config.tablename + "" + " where "
						+ targetField_1 + " like '%" + keyword + "%' " + " or "
						+ targetField_2 + " like '%" + keyword + "%'"
						+ s1.join('') + s2.join('');
	}	
	,highlight : function(str, arrKeywords) {
		// Generate Keyword RegExp String - We need to duplicate the original
		// keyword array here
		var reStr = arrKeywords.join("$,$");
		reStr = reStr.split("$,$");
		for (var i = 0; i < reStr.length; i++) {
			if (func.lengthW(reStr[i]) < 3) {
				reStr.splice(i, 1);
				i--;
			} else {
				reStr[i] = this.stringToRegExp(reStr[i]);
			}
		}
		reStr = new RegExp("(" + reStr + ")", "ig");
		
		var arrMatch;
		var re = new RegExp("(\>|^)(.*?)(\<|$)", "igm");
		while ((arrMatch = re.exec(str)) != null) {
			var newStr = arrMatch[1]
					+ arrMatch[2].replace(reStr, '<span class="highlight">'
									+ "$1" + '</span>') + arrMatch[3];
			str = str.replace(arrMatch[0], newStr);
			re.lastIndex += newStr.length - arrMatch[0].length;
		}

		return str;
	}

};

$$.sql.Searcher.actions = {};
$$.sql.Searcher.actions.get = function(){
	var folder = "e:\\edk";
	var isInvert = false;
	var openFileFn = $$.disk.toString;
	var 
		 wildcard = 'partial' || "*"
		,wildcard = new RegExp(
//				"^" +
					 (
						wildcard
						.replace(/([\\\^\$+[\]{}.=!:(|)])/g, "\\$1") // 转义为字符串表达方式RegExp
						.replace(/\*/g, ".*") // 通配符转RegExp
						.replace(/\?/g, ".")  // 通配符转RegExp
					)
//				+ "$"
				,'ig'
			);	
	var fn = function(fileObj, folderObj){
		var fileContent = openFileFn(fileObj.path);
		if(fileContent){
			if(wildcard.test(fileContent) && !isInvert){
				l(fileObj+'<br>');
			}else if(isInvert && !wildcard.test(fileContent)){
				// isInvert goes here……
			}
		}else{
			// 文件内容为空
		}
	};
	var isRecursive = true;
	var f = $$.disk.getFlatFlies(folder, isRecursive, fn);	
}